package com.yanqu.road.server.cmd.estatewar.activity.cross;

import com.yanqu.road.entity.estatewar.palace.UserPalaceData;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.palace.PalaceModule;
import com.yanqu.road.server.manger.palace.PalaceActivity;
import com.yanqu.road.server.manger.palace.PalaceActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_PALACE_AIDE_JOIN_SETTING_RESP, desc = "幕宾加入设置响应")
public class CrossPalaceAideJoinSettingResponseCmd extends AbstractServerCommand {

    private long userId;
    private int result;
    private int activityId;
    private boolean needResponse;
    private boolean onlyUnionMemberJoin;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceAideJoinSettingCrossRespMsg reqMsg = EstateWarProto.PalaceAideJoinSettingCrossRespMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        activityId = reqMsg.getActivityId();
        result = reqMsg.getRet();
        needResponse = reqMsg.getNeedResponse();
        if(0 == result){
            onlyUnionMemberJoin = reqMsg.getOnlyUnionMemberJoin();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(0 == result){
            PalaceActivity palaceActivity = PalaceActivityMgr.getPalaceActivity(activityId);
            if(null != palaceActivity){
                UserPalaceData userPalaceData = palaceActivity.getUserPalaceData(userId);
                if(null != userPalaceData){
                    userPalaceData.setOnlyUnionMemberJoin(onlyUnionMemberJoin);
                    if(null != player){
                        player.getModule(PalaceModule.class).syncUserData();
                    }
                }
            }
        }else {
            ret = result;
        }

        if(needResponse) {
            if(null != player){
                EstateWarProto.PalaceAideJoinSettingRespMsg.Builder respMsg = EstateWarProto.PalaceAideJoinSettingRespMsg.newBuilder();
                respMsg.setRet(ret);
                player.sendPacket(Protocol.U_PALACE_AIDE_JOIN_SETTING, respMsg);
            }
        }
    }

    @Override
    public void sendData(YanQuMessage packet) throws Exception {

    }
}
